package com.mvc.servlet;

import java.io.IOException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mvc.core.util.InitAnnotation;
import com.mvc.core.util.ReflectUtil;
import com.mvc.core.util.Request;

public class DispatcherServlet extends HttpServlet{

	private static final long serialVersionUID = 898796189975659247L;
	
	private Map<String,Request> requestMap = null;
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		System.out.println("初始化");
		requestMap = InitAnnotation.initConfig(); //初如化加载注解数据
		System.out.println("requestMap=="+requestMap);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doGet(req, resp);
	}
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path = request.getServletPath();
//		System.out.println("path=="+path);
//		path = path.split("\\.")[0];
		try {
			Request requestObj = this.requestMap.get(path);
//			System.out.println("requestObj=="+requestObj);
			Object traget = ReflectUtil.invoke(requestObj,request);
			
//			ProxyFactory proxyFactory = new ProxyFactory();
//			proxyFactory.setRequest(request);
//			proxyFactory.setTraget(traget);
//			Object proxy = proxyFactory.getProxy();
//			request = (HttpServletRequest) proxy;
//			request.getParameter("");
			RequestDispatcher dispatcher = request.getRequestDispatcher(traget.toString());
			dispatcher.forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	
	
	

}
